﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EveMarketsDAO;

namespace CoreEveMarkets
{
	public class Invention
	{
		#region Attributs

		private readonly List<Composant> _listeComposants;
		private readonly int _idBlueprintTech2;
		private readonly int _idBlueprintOrigine;

		#endregion

		#region Get&Set

		/// <summary>
		/// Numéro de l'objet Tech2
		/// </summary>
		public int IdBlueprintTech2
		{
			get { return _idBlueprintTech2; }
		}

		/// <summary>
		/// Numéro du Blueprint origine
		/// </summary>
		public int IdBlueprintOrigine
		{
			get { return _idBlueprintOrigine; }
		}

		#endregion

		#region Constructeur

		/// <summary>
		/// 
		/// </summary>
		/// <param name="numObjetTech2"></param>
		/// <param name="numObjetOrigine"></param>
		/// <param name="listePourInvention"></param>
		public Invention(int numObjetTech2, int numObjetOrigine, List<ComposantsBlueprint> listePourInvention)
		{
			_idBlueprintTech2 = numObjetTech2;
			_idBlueprintOrigine = numObjetOrigine;
			_listeComposants = new List<Composant>(listePourInvention.Capacity);

			foreach (var materiel in listePourInvention)
			{
				Composant compo = new Composant(materiel.ComposantId, materiel.LiensVers_TypeId_pour_CompoId.typeName, materiel.Quantite);
				_listeComposants.Add(compo);
			}
		}

		#endregion

		#region Méthodes Public

		/// <summary>
		/// Calcul le pourcentage de réussite d'un Blueprint Tech2.
		/// </summary>
		/// <returns></returns>
		public double CalculPourcentageReussite(short encryptionLvl = 4, short skillDataCoreUn = 4, short skillDataCoreDeux = 4, short metaLvlTechI = 0)
		{
			/*
			 * Invention_Chance = Base_Chance * (1 + (0.01 * Encryption_Skill_Level)) * (1 + ((Datacore_1_Skill_Level + Datacore_2_Skill_Level) * (0.1 / (5 - Meta_Level)))) * Decryptor_Modifier
			 * The variables are:
			 * Base_Chance
			 * Modules and Ammo have a base probability of 40%
			 * Frigates, Destroyers, Freighters and Skiff have a base probability of 30%
			 * Cruisers, Industrials and Mackinaw have a base probability of 25%
			 * Battlecruisers, Battleships and Hulk have a base probability of 20%
			 */
			//NOTE : Pour l'instant je ne prend pas en compte les Decryptors.
			
			return this.DeterminationPourcentageBase(IdBlueprintOrigine) * ((1 + (0.01 * encryptionLvl)) * (1 + ((skillDataCoreUn + skillDataCoreDeux)) * (0.1 / (5 - metaLvlTechI))));
		} 

		
		#endregion

		#region Méthodes Private

		/// <summary>
		/// Détermination du pourcentage de base de réussite. Dépend du BP tech1.
		/// </summary>
		/// <param name="numeroBlueprintOrigine"></param>
		/// <returns></returns>
		private double DeterminationPourcentageBase(int numeroBlueprintOrigine)
		{
			double retourBaseChance = 0;
			/* Base - Basechance :
			 * Modules and Ammo have a base probability of 40%
			 * Frigates, Destroyers, Freighters and Skiff have a base probability of 30%
			 * Cruisers, Industrials and Mackinaw have a base probability of 25%
			 * Battlecruisers, Battleships and Hulk have a base probability of 20%
			 */
			var typeObjet = ContexteDAO.FonctionsBasique.GetTypeItemByBlueprint(numeroBlueprintOrigine);

			switch (typeObjet)
			{
				case "Frigate":
				case "Destroyer":
				case "Freighter":
					{
						retourBaseChance = 0.3;
						break;
					}
				case "Cruiser":
				case"Industrial":
					{
						retourBaseChance = 0.25;
						break;
					}
				case "Battlecruiser":
				case "Battleship":
					{
						retourBaseChance = 0.2;
						break;
					}
				default:
					{
						if(TestSurShipMineur(numeroBlueprintOrigine))
						{
							retourBaseChance = TrouverBaseChanceShipMineur(numeroBlueprintOrigine);
							break;
						}
						
						// Par défaut les modules et les ammos sont à 40%.
						retourBaseChance = 0.4;
						break;
					}
			}

			return retourBaseChance;
		}

		/// <summary>
		/// Trouve la chance de base de réussite pour les ships mineurs.
		/// </summary>
		/// <param name="numeroBlueprintOrigine"></param>
		/// <returns></returns>
		private double TrouverBaseChanceShipMineur(int numeroBlueprintOrigine)
		{
			/*
			 * idBlueprint = 17477 --> Hulk
			 * idBlueprint = 17481 --> Skiff
			 * idBlueprint = 17479 --> Mackinaw
			 */
			double retourChance = 0;

			switch (numeroBlueprintOrigine)
			{
				case 17477:
					{
						retourChance = 0.2;
						break;
					}
				case 17481 :
					{
						retourChance = 0.3;
						break;
					}
				case 17479:
					{
						retourChance = 0.25;
						break;
					}
			}
			return retourChance;
		}

		/// <summary>
		/// Test pour voir si le blueprint passé en paramètre est dans la liste des blueprints "Mineur".
		/// </summary>
		/// <param name="numeroBlueprint"></param>
		/// <returns></returns>
		private bool TestSurShipMineur(int numeroBlueprint)
		{
			List<int> listeBlueprintMineur = new List<int>{17477,17481,17479};

			return listeBlueprintMineur.Contains(numeroBlueprint);
		}

		#endregion
	}
}
